var discuss_cur={
	constant:{
		pageNo:1,
		pageSize:15,
	}
}
var tale = new $.tale();//弹框
;$(function() {
	var id = $("#id").val();
	save_discuss();
	getUserList(id);
})

//分页查询
function getDiscussList(){
	var supervisionId = $("#id").val();
		$.ajax({
	    	url: ctxPath+"/supervise/discuss/getPage",
	        type: 'POST',
	        data: {'supervisionId':supervisionId,pageNo:discuss_cur.constant.pageNo,pageSize:discuss_cur.constant.pageSize},
	        success: function(d){
	        	 if(d.data!=null){
	        		 var html="";
	        	$.each(d.data.dataList,function(i,item){
	        		html+=toDiscussHtml(item);
	        	})	
	        	$("#discuss_result").html(html);
	        	} 
	        getDiscussPage(d.data.pages,d.data.total);	 
	        }
	    });
	}

 function save_discuss(){
	 $("#discuss_form").validator({
	        rules: {
	            check_discuss:function(element){
		    		var name = element.name;
		    		var json={};
		    		   json["discuss"] = element.value;
		    		  var value = $("#id").val();
		    		  json["supervisionId"] = value;
		    		  var discuss_value = $("#discuss_id").val();
		    		  if(discuss_value!=null&&discuss_value!=''){
		    			  var id="id";
		    			  json[id] = discuss_value;
		    		  }
		    		var isValid = true;
		        	$.ajax({
		        		 url: ctxPath+'/supervise/discuss/checkdiscuss',
		                 type: 'post',
		                 data: json,
		                 dataType: 'json',
		                 async:false,
		                 success: function(d){
		                	 isValid =  d;
		                 }
		        	})
		        	return isValid|| "进度超过100%了";
		    	}
	        },
	        fields: {
	        	discuss: {
	                rule: "required;range(1~100);check_discuss",
	                msg: {
	                    required: "必填!"
	                }
	            },
	            time: {
	                rule: "required;checked",
	                msg: {
	                    required: "必填!"
	                }
	            },
	            'userId': {
	                rule: "required;",
	                msg: {
	                    required: "必填!"
	                }
	            },
	            'content': {
	            	rule: "required;length(~200, true);",
	            	msg: {
	            		required: "必填!"
	            	}
	            },
	        },
	        invalid: function (form, errors) {
	            console.log(errors);
	            $(".btn-submit").removeAttr("style");
	        },
	        //验证成功
	        valid: function (form) {
	            saveInfo();
	        },
	    });
 }

 function saveInfo() {
	    var json = $("#discuss_form").serializeFormJSON();
	    console.log(json);
	    var url = ctxPath+"/supervise/discuss/saveInfo";
	    $.ajax({
	        url: url,
	        type: 'POST',
	        data: json,
	        success: function (d) {
	            var success = d.success;
	            if (success) {
	                tale.alertOk({
	                    text: '保存成功',
	                    then: function () {
	                        setTimeout(function () {
	                        	$("#discussModal").modal('hide');
	                        	getDiscussList();
	                        }, 500);
	                    }
	                });
	            } else {
	                tale.alertError(d.message || '保存失败');
	            }
	        }
	    });
	}
 
	//分页
	function getDiscussPage(pageCount,totalCount){
		kkpager2.total =pageCount ;//总页码
		kkpager2.totalRecords = totalCount;//总条数
		kkpager2.generPageHtml({
			pno : 0,
			mode : 'click',//默认值是link，可选link或者click
			click : function(n){
				discuss_cur.constant.pageNo = n;
				gediscussList();
				this.selectPage(n);
			    return false;
			}
		});
	}
	
	
	function addDiscuss(){
		var supervisionId = $("#id").val();
		$("#discussModal").modal('show');
	}
	
	
	
	/**
	 * 获取机构下的人员
	 * @returns
	 */
	function getUserList(id){
		var result ='<option value="">所有人</option>';
		$.ajax({
			url: ctxPath+"/supervise/user/getUsers/"+id,
			type: 'GET',
			success: function(d){
				var success = d.success;
				if(success==true){
					if(d.data!=null){
						$.each(d.data,function(i,item){
							result+='<option value="'+item.userId+'">'+item.nickname+'</option>';	
						})
						$("#discuss_user_select").html(result);
					}	 
				}
				
			}
		})
	}
	
	
	function toDiscussHtml(data){
		var toNickname = data.toNickname;
		if(toNickname==null){
			toNickname="@所有人";
		}else{
			toNickname="@"+toNickname;
		}
		 return '<tr>'
	         +'<td>'
	         +data.nickname
	         +'</td>'
	         +'<td>'
	         +toNickname
	         +'</td>'
	         +'<td>'
	         +data.content
	         +'</td>'
	         +'<td>'
	         +data.createTime
	         +'</td>'
	        /* +'<td>'
	         +'<button type="button" class="btn btn-danger btn-sm waves-effect waves-light m-b-5" onclick="del_discuss(' + data.id + ')"><i class="fa fa-trash-o"></i> 删除</button>'
	         +'</td>'*/
	         +'</tr>';
	 }
	
	 
   function del_discuss(id,name){
	    tale.alertConfirm({
	        title:'确定删除吗?',
	        then: function () {
	            tale.post({
	                url : ctxPath+'/supervise/discuss/delInfo',
	                data: {id: id},
	                success: function (result) {
	                    if(result && result.success == true){
	                    	getDiscussList();
//	                        tale.alertOkAndReload('删除成功');
	                    } else {
	                        tale.alertError(result.msg || '删除失败');
	                    }
	                }
	            });
	        }
	    });
	 }